package com.ctg.behavior.calc.impala.builder.userbehavior;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtCalcDate;
import com.ctg.behavior.calc.impala.report.userbehavior.CtUBReport;
import com.ctg.behavior.calc.utils.CtCalcDaysUtils;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.userbehavior.CtUBCalcBody;
import com.ctg.behavior.utils.CtDateUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUBReportBuilder {

	public List<CtUBReport> doBuilder(CtUBCalcBody calcBody) {

		log.info("calcBody={}", JSON.toJSONString(calcBody));

		Integer reportIndex = 0;
		List<CtUBReport> calcReports = new ArrayList<>();

		String loadType = calcBody.getLoadType();
		String fromDate = calcBody.getFromDate();
		String toDate = calcBody.getToDate();
		String timeUnit = calcBody.getUnit();
		// String todayDate = CtDateUtils.getCurrentDate(CtDateUtils.YYYY_MM_DD);
		log.info("loadType={},fromDate={},toDate={},timeUnit={}", loadType, fromDate, toDate, timeUnit);

		if (CtCalcConsts.LOAD_TYPE_EVENT_GROUPBY.equalsIgnoreCase(loadType)) {
			// 事件分组统计 - 按事件分组维度统计

			CtUBReport oneReport = this.copyBody(calcBody);
			calcReports.add(oneReport);
			oneReport.setReportIndex(reportIndex++);
			
			Date eventFromTime = CtDateUtils.stringToDate(calcBody.getFromDate(), CtDateUtils.YYYY_MM_DD);
//			oneReport.setEventFromTime(eventFromTime.getTime());
//			oneReport.setEventFromDate(calcBody.getFromDate());
			oneReport.getCalcDate().setFromDate(eventFromTime);

			Date eventToTime = CtDateUtils.stringToDate(calcBody.getToDate(), CtDateUtils.YYYY_MM_DD);
			eventToTime = CtDateUtils.formatDayEndTime(eventToTime);
//			oneReport.setEventToTime(eventToTime.getTime());
//			oneReport.setEventToDate(calcBody.getToDate());
			oneReport.getCalcDate().setToDate(eventToTime);

		} else if (CtCalcConsts.LOAD_TYPE_EVENT_TOTAL.equalsIgnoreCase(loadType)) {
			// 事件总量统计 - 按时间维度统计
			if (CtCalcConsts.UNIT_TYPE_HOUR.equalsIgnoreCase(timeUnit)) {
				List<CtCalcDate> hours = CtCalcDaysUtils.listCalcHours(fromDate, toDate);
				for (CtCalcDate oneHour : hours) {
					CtUBReport oneReport = this.copyBody(calcBody);
					calcReports.add(oneReport);

//					Date hourDate = CtDateUtils.stringToDate(oneHour);
//					oneReport.setEventFromTime(hourDate.getTime());
//					oneReport.setEventFromDate(CtDateUtils.dateToString(hourDate));

//					hourDate = CtDateUtils.formatHourEndTime(hourDate);
//					oneReport.setEventToTime(hourDate.getTime());
//					oneReport.setEventToDate(CtDateUtils.dateToString(hourDate));
					oneReport.setCalcDate(oneHour);

					oneReport.setReportIndex(reportIndex++);
				}
			} else if (CtCalcConsts.UNIT_TYPE_DAY.equalsIgnoreCase(timeUnit)) {
				List<String> days = CtCalcDaysUtils.listCalcDayList(fromDate, toDate);
				for (String day : days) {
					CtUBReport oneReport = this.copyBody(calcBody);
					calcReports.add(oneReport);

					Date dayDate = CtDateUtils.stringToDate(day, CtDateUtils.YYYY_MM_DD);
//					oneReport.setEventFromTime(dayDate.getTime());
//					oneReport.setEventFromDate(CtDateUtils.dateToString(dayDate));
					oneReport.getCalcDate().setFromDate(dayDate);

					dayDate = CtDateUtils.formatDayEndTime(dayDate);
//					oneReport.setEventToTime(dayDate.getTime());
//					oneReport.setEventToDate(CtDateUtils.dateToString(dayDate));
					oneReport.getCalcDate().setToDate(dayDate);

					oneReport.setReportIndex(reportIndex++);
				}
			} else if (CtCalcConsts.UNIT_TYPE_WEEK.equalsIgnoreCase(timeUnit)) {
				List<CtCalcDate> weeks = CtCalcDaysUtils.listCalcWeeks(fromDate, toDate);
				for (CtCalcDate week : weeks) {
					CtUBReport oneReport = this.copyBody(calcBody);
					calcReports.add(oneReport);

//					oneReport.setEventFromTime(CtDateUtils.stringToDate(week.getFromDate(), CtDateUtils.YYYY_MM_DD).getTime());
//					oneReport.setEventFromDate(week.getFromDate());
					oneReport.getCalcDate().setFromDate(week.getFromDate());

//					oneReport.setEventToTime(CtDateUtils.stringToDate(week.getToDate(), CtDateUtils.YYYY_MM_DD).getTime());
//					oneReport.setEventToDate(week.getToDate());
					oneReport.getCalcDate().setFromDate(week.getToDate());

					oneReport.setReportIndex(reportIndex++);
				}

			} else if (CtCalcConsts.UNIT_TYPE_MONTH.equalsIgnoreCase(timeUnit)) {
				List<CtCalcDate> months = CtCalcDaysUtils.listCalcMonths(fromDate, toDate);
				for (CtCalcDate month : months) {
					CtUBReport oneReport = this.copyBody(calcBody);
					calcReports.add(oneReport);

//					oneReport.setEventFromTime(CtDateUtils.stringToDate(month.getFromDate(), CtDateUtils.YYYY_MM_DD).getTime());
//					oneReport.setEventFromDate(month.getFromDate());
					oneReport.getCalcDate().setFromDate(month.getFromDate());

//					oneReport.setEventToTime(CtDateUtils.stringToDate(month.getToDate(), CtDateUtils.YYYY_MM_DD).getTime());
//					oneReport.setEventToDate(month.getToDate());
					oneReport.getCalcDate().setFromDate(month.getToDate());

					oneReport.setReportIndex(reportIndex++);
				}

			}
		}

		log.info("calcReports={}", JSON.toJSONString(calcReports));

		return calcReports;

	}

	public CtUBReport copyBody(CtUBCalcBody calcBody) {

		CtUBReport report = new CtUBReport();

		report.setRequestId(calcBody.getRequestId());
		report.setAppId(calcBody.getAppId());
		report.setBookmarkId(calcBody.getBookmarkId());

		report.setUserId(calcBody.getUserId());
		report.setDeviceId(calcBody.getDeviceId());
		report.setFromDate(calcBody.getFromDate());
		report.setToDate(calcBody.getToDate());
		report.setUnit(calcBody.getUnit());

		report.setByFields(calcBody.getByFields());
		report.setRollupDate(calcBody.getRollupDate());
		report.setFilter(calcBody.getFilter());
		report.setMeasures(calcBody.getMeasures());

		report.setLoadType(calcBody.getLoadType());

		return report;

	}

}
